const dnmodel = [
  {
    title: "天天送",
    zhouqi: '0'
  },
  {
    title: "隔日送",
    zhouqi: '1'
  },
  {
    title: "三日送",
    zhouqi: '2'
  },
  {
    title: "单日送",
    zhouqi: '3'
  },
  {
    title: "双日送",
    zhouqi: '4'
  },
  {
    title: "工作日送",
    zhouqi: '5'
  },
  {
    title: "周末送",
    zhouqi: '6'
  },
  {
    title: "单次购",
    zhouqi: '7'
  },
]

const select_jzrq = dn => {
  if (Number(dn.mints) > 0) dn.isjzrq = 1
}
const un_select_jzrq = dn => {
  if (Number(dn.mints) > 0) dn.isjzrq = 0
}
/**
 * dn:mints(n固定天数模式,-1月底模式,-2自然月模式),jsmodel(JINE，TIANSHU),isjzrq(选择截止日期),pstype(0同送，1轮送),
 * zhouqi(0天天订，1单双日订，2周一至周五，3周末订)
 * Count,qdrq,tdrq,jgts,dsj,TotalDays,TotalCount,je,
 * xq1,xq2,xq3,xq4,xq5,xq6,xq7
 */
const JisuanModel = dn => {
  if (dn.TotalDays == 0) dn.TotalDays = Number(dn.mints)
  if (dn.mints == '-1') {

  } else if (Number(dn.mints) >= 0) {
    dn = TongsongNdays(dn)
  } else if (dn.mints == '-2') {

  }

  return dn;
}

//轮送+天天订（pstype{2}+dnlx{0}+jgts{0}）
//轮送+隔天订（pstype{2}+dnlx{0}+jgts{1}）
//轮送+星期订（pstype{2}+dnlx{1}）
//同送+天天订（pstype{1}+dnlx{0}+jgts{0}）
//同送+隔天订（pstype{1}+dnlx{0}+jgts{1}）
//同送+星期订（pstype{1}+dnlx{1}）
//【间隔订】只实现1种，1、间隔1天订
//【星期订】只实现2种，1、周一至周五订；2、周末订
/**订购天数固定 */
const TongsongNdays = dn => {
  //天天订、隔日订
  if (dn.zhouqi == '0' || dn.zhouqi == '1' || dn.zhouqi == '2' || dn.zhouqi == '3' || dn.zhouqi == '4' || dn.zhouqi == '7') {
    dn = ZhouqiTongsongNdays(dn)
  } else if (dn.zhouqi == '5' || dn.zhouqi == '6') { //周一至五、周末订
    dn = XingqiTongsongNdays(dn)
  }
  return dn;
}

//周期同送
const ZhouqiTongsongNdays = dn => {
  if (dn.zhouqi == '3') { //单日送
    var qdrq = GetDateObject(dn.qdrq)
    if (qdrq.getDate() % 2 == 0) dn.qdrq = addDate(qdrq, 1)
  } else if (dn.zhouqi == '4') { //双日送
    var qdrq = GetDateObject(dn.qdrq)
    if (qdrq.getDate() % 2 == 1) dn.qdrq = addDate(qdrq, 1)
  }

  if (dn.zhouqi == '7') { //单次购
    dn.tdrq = dn.qdrq
    dn.TotalDays = 1
  } else {
    var jz = GetZhouqidingJzrq(dn)
    dn.tdrq = formatDate(jz)
  }
  dn.TotalCount = dn.TotalDays * dn.Count
  dn.je = (dn.TotalCount * Number(dn.dsj)).toFixed(2)
  return dn;
}

//星期同送
const XingqiTongsongNdays = dn => {
  dn.jgts = 0;
  if (dn.zhouqi == '5') {
    dn.xq1 = Number(dn.Count);
    dn.xq2 = dn.xq1;
    dn.xq3 = dn.xq1;
    dn.xq4 = dn.xq1;
    dn.xq5 = dn.xq1;
    dn.xq6 = 0;
    dn.xq7 = 0;
  } else if (dn.zhouqi == '6') {
    dn.xq1 = 0;
    dn.xq2 = dn.xq1;
    dn.xq3 = dn.xq1;
    dn.xq4 = dn.xq1;
    dn.xq5 = dn.xq1;
    dn.xq6 = Number(dn.Count);
    dn.xq7 = dn.xq6;
  }
  dn = GetXingqiding(dn)
  //var dtList = GetSnrqListNdays(dn.qdrq, dn.TotalDays, dn.xq1, dn.xq2, dn.xq3, dn.xq4, dn.xq5, dn.xq6, dn.xq7);
  dn.qdrq = formatDate(dn.qdrq);
  dn.tdrq = formatDate(dn.tdrq);
  dn.TotalCount = Number(dn.TotalDays) * Number(dn.Count);
  dn.je = toDecimal2(dn.TotalCount * dn.dsj);
  return dn;
}
const GetXingqiding = dn => {
  if (dn.isjzrq) {
    var snrqList = GetSnrqList2(dn.qdrq, dn.tdrq, dn.xq1, dn.xq2, dn.xq3, dn.xq4, dn.xq5, dn.xq6, dn.xq7)
    dn.qdrq = snrqList[0]
    dn.tdrq = snrqList[snrqList.length - 1]
    dn.TotalDays = snrqList.length
    return dn
  }
  var dnts = 0
  if (dn.ishuodong) {
    if (dn.onsale.jsmodel == "JINE") {
      if (dn.ists) {} else {
        dnts = Math.ceil(dn.onsale.dgje / dn.dsj) > dn.TotalDays ? Math.ceil(dn.onsale.dgje / dn.dsj) : dn.TotalDays
        dn.TotalDays = dnts
      }
    } else if (dn.onsale.jsmodel == "TIANSHU") {
      if (dn.ists) {} else {
        dnts = dn.onsale.dgje > dn.TotalDays ? dn.onsale.dgje : dn.TotalDays
        dn.TotalDays = dnts
      }
    }
  } else {
    dnts = dn.TotalDays
  }
  var dtList = GetSnrqListNdays(dn.qdrq, dn.TotalDays, dn.xq1, dn.xq2, dn.xq3, dn.xq4, dn.xq5, dn.xq6, dn.xq7);
  dn.qdrq = dtList[0]
  dn.tdrq = dtList[dtList.length - 1]
  return dn
}
// ishuodong: false,
// ists: true,
// isksrq: false,
// isjzrq: false,
const GetZhouqidingJzrq = dn => {
  if (dn.isjzrq) {
    dn.TotalDays = DingnaiBetweenDays(dn.qdrq, dn.tdrq, dn.jgts)
    return GetDateObject(dn.tdrq)
  }
  var dnts = 0
  if (dn.ishuodong) {
    if (dn.onsale.jsmodel == "JINE") {
      if (dn.ists) dnts = dn.TotalDays
      else dnts = Math.ceil(dn.onsale.dgje / dn.dsj) > dn.TotalDays ? Math.ceil(dn.onsale.dgje / dn.dsj) : dn.TotalDays
    } else if (dn.onsale.jsmodel == "TIANSHU") {
      if (dn.ists) dnts = dn.TotalDays
      else dnts = dn.onsale.dgje > dn.TotalDays ? dn.onsale.dgje : dn.TotalDays
    }
  } else {
    dnts = dn.TotalDays
  }

  var jsrq = GetDateObject(dn.qdrq)
  const jgts = Number(dn.jgts)
  for (var i = 0; i < dnts - 1; i++) {
    jsrq = addDate(jsrq, jgts + 1)
    jsrq = GetDateObject(jsrq)
  }
  return jsrq
}

const DingnaiBetweenDays = (StartDate, EndDate, jgts) => {
  jgts = Number(jgts);
  var days = DateDiff("day", EndDate, StartDate);
  var value = (days + 1) / (jgts + 1);
  var DNDays = Math.floor(value);
  var ys = (days + 1) % (jgts + 1);
  if (ys > 0)
    DNDays += 1;
  return DNDays;
}
const GetSnrqListNdays = (StartDate, DNDays, xq1, xq2, xq3, xq4, xq5, xq6, xq7) => {
  var snrqiList = new Array();
  var dt = StartDate;
  console.log(dt);
  dt = formatDate(DateAdd("day", -1, dt));
  for (var i = 0; i < DNDays; i++) {
    dt = DateAdd("day", 1, dt);
    if ((xq1 != 0 && dt.getDay() == 1) ||
      (xq2 != 0 && dt.getDay() == 2) ||
      (xq3 != 0 && dt.getDay() == 3) ||
      (xq4 != 0 && dt.getDay() == 4) ||
      (xq5 != 0 && dt.getDay() == 5) ||
      (xq6 != 0 && dt.getDay() == 6) ||
      (xq7 != 0 && dt.getDay() == 0)) {
      //alert(dt.getDay());
      snrqiList.push(dt);
    } else
      i--;

    dt = formatDate(dt);
  }
  return snrqiList;
}
/**星期订奶日期队列 */
const GetSnrqList2 = (StartDate, EndDate, xq1, xq2, xq3, xq4, xq5, xq6, xq7) => {
  var DNDays = DingnaiBetweenDays(StartDate, EndDate, 0);
  var snrqiList = new Array();

  for (var i = 0; i < DNDays; i++) {
    var dt = new Date();
    dt = DateAdd("day", i * (0 + 1), StartDate);

    if ((xq1 != 0 && dt.getDay() == 1) ||
      (xq2 != 0 && dt.getDay() == 2) ||
      (xq3 != 0 && dt.getDay() == 3) ||
      (xq4 != 0 && dt.getDay() == 4) ||
      (xq5 != 0 && dt.getDay() == 5) ||
      (xq6 != 0 && dt.getDay() == 6) ||
      (xq7 != 0 && dt.getDay() == 0)) {
      //alert(dt.getDay());
      snrqiList.push(dt);
    }
  }

  return snrqiList;
}


const addDate = (date, days) => {
  var d = date;
  d.setDate(d.getDate() + (days));
  var month = d.getMonth() + 1;
  var day = d.getDate();
  if (month < 10) {
    month = "0" + month;
  }
  if (day < 10) {
    day = "0" + day;
  }
  var val = d.getFullYear() + "-" + month + "-" + day;
  return val;
}
const DateAdd = (interval, num, dateValue) => {
  var newdate = addDate(GetDateObject(dateValue), num);
  return GetDateObject(newdate);
}
const GetDateObject = dateValue => {
  //兼容IE6.7.8.
  var s = dateValue;
  var ps = s.split(" ");
  var pd = ps[0].split("-");
  var pt = ps.length > 1 ? ps[1].split(":") : [0, 0, 0];
  var newCom = new Date(pd[0], pd[1] - 1, pd[2], pt[0], pt[1], pt[2]);
  return newCom;
}
const TimeCom = dateValue => {
  var newCom = GetDateObject(dateValue);
  return {
    year: newCom.getFullYear(), //年份2000以后用，getYear返回三位数字
    month: newCom.getMonth() + 1,
    day: newCom.getDate(),
    hour: newCom.getHours(),
    minute: newCom.getMinutes(),
    second: newCom.getSeconds(),
    msecond: newCom.getMilliseconds(),
    week: newCom.getDay(),
  };
}
const DateDiff = (interval, date1, date2) => {
  var TimeCom1 = new TimeCom(date1);
  var TimeCom2 = new TimeCom(date2);
  var result;
  switch (String(interval).toLowerCase()) {
    case "y":
    case "year":
      result = TimeCom1.year - TimeCom2.year;
      break;
    case "n":
    case "month":
      result = (TimeCom1.year - TimeCom2.year) * 12 + (TimeCom1.month - TimeCom2.month);
      break;
    case "d":
    case "day":
      result = Math.round((Date.UTC(TimeCom1.year, TimeCom1.month - 1, TimeCom1.day) - Date.UTC(TimeCom2.year, TimeCom2.month - 1, TimeCom2.day)) / (1000 * 60 * 60 * 24));
      break;
    case "h":
    case "hour":
      result = Math.round((Date.UTC(TimeCom1.year, TimeCom1.month - 1, TimeCom1.day, TimeCom1.hour) - Date.UTC(TimeCom2.year, TimeCom2.month - 1, TimeCom2.day, TimeCom2.hour)) / (1000 * 60 * 60));
      break;
    case "m":
    case "minute":
      result = Math.round((Date.UTC(TimeCom1.year, TimeCom1.month - 1, TimeCom1.day, TimeCom1.hour, TimeCom1.minute) - Date.UTC(TimeCom2.year, TimeCom2.month - 1, TimeCom2.day, TimeCom2.hour, TimeCom2.minute)) / (1000 * 60));
      break;
    case "s":
    case "second":
      result = Math.round((Date.UTC(TimeCom1.year, TimeCom1.month - 1, TimeCom1.day, TimeCom1.hour, TimeCom1.minute, TimeCom1.second) - Date.UTC(TimeCom2.year, TimeCom2.month - 1, TimeCom2.day, TimeCom2.hour, TimeCom2.minute, TimeCom2.second)) / 1000);
      break;
    case "ms":
    case "msecond":
      result = Date.UTC(TimeCom1.year, TimeCom1.month - 1, TimeCom1.day, TimeCom1.hour, TimeCom1.minute, TimeCom1.second, TimeCom1.msecond) - Date.UTC(TimeCom2.year, TimeCom2.month - 1, TimeCom2.day, TimeCom2.hour, TimeCom2.minute, TimeCom2.second, TimeCom1.msecond);
      break;
    case "w":
    case "week":
      result = Math.round((Date.UTC(TimeCom1.year, TimeCom1.month - 1, TimeCom1.day) - Date.UTC(TimeCom2.year, TimeCom2.month - 1, TimeCom2.day)) / (1000 * 60 * 60 * 24)) % 7;
      break;
    default:
      result = "invalid";
  }
  return (result);
}

const toDecimal2 = x => {
  var f = parseFloat(x);
  if (isNaN(f)) {
    return false;
  }
  var f = Math.round(x * 100) / 100;
  var s = f.toString();
  var rs = s.indexOf('.');
  if (rs < 0) {
    rs = s.length;
    s += '.';
  }
  while (s.length <= rs + 2) {
    s += '0';
  }
  return s;
}
const formatDate = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()

  return [year, month, day].map(formatNumber).join('-')
}
const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

module.exports = {
  JisuanModel: JisuanModel,
  dnmodel: dnmodel
}